/*
 * XCompositeEditor.java
 * 
 * Copyright (c) 2006 - 2007
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xfc.editor;

import java.util.ArrayList;
import java.util.List;

import javax.swing.Icon;
import javax.swing.JLabel;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

/**
 * 
 * 
 * @author Devon Carew
 */
public class XCompositeEditor
	extends XEditor
{
	private List editors = new ArrayList();
	
	XEditorListener editorListener = new XEditorListener() {
		public void valueChanged(XEditorEvent event) {
			editorChanged(event);
		}
	};
	
	/**
	 * 
	 */
	public XCompositeEditor()
	{
		super();
		
		setLayout(new FormLayout(
			"pref 3dlu pref:grow",
			"pref"
		));
	}
	
	/**
	 * 
	 * 
	 * @param title
	 */
	public XCompositeEditor(String title)
	{
		this();
		
		setTitle(title);
	}
	
	/**
	 * 
	 * 
	 * @param title
	 * @param icon
	 */
	public XCompositeEditor(String title, Icon icon)
	{
		super(title, icon);
	}
	
	/**
	 * Add the editor to this composite editor.
	 * 
	 * @param editor
	 */
	public void addEditor(XEditor editor)
	{
		editors.add(editor);
		
		editor.addEditorListener(editorListener);
		
		addEditorToLayout(editor);
	}
	
	protected void addEditorToLayout(XEditor editor)
	{
		String title = editor.getTitle();
		
		FormLayout layout = (FormLayout)getLayout();
		
		int row = layout.getRowCount();
		
		layout.insertRow(row, new RowSpec("pref"));
		
		if (title != null)
			add(new JLabel(title + ":"), "1, " + row + ", r, t");
		
		// left aligned or full?
		add(editor, "3, " + row + ", l, t");
		
		layout.insertRow(row + 1, new RowSpec("2dlu"));
	}
	
	// TODO: have a remove editor method?
	
	
	/**
	 * @return the editors contained in this composite editor
	 */
	public List getEditors()
	{
		return editors;
	}
	
	/**
	 * This method is not supported for XCompositeEditors.
	 */
	public Object getValue()
	{
		throw new UnsupportedOperationException();
	}

	/**
	 * This method is not supported for XCompositeEditors.
	 */
	public void setValue(Object value)
	{
		throw new UnsupportedOperationException();
	}
	
	protected void editorChanged(XEditorEvent event)
	{
		// TODO: we need the idea of editor validity
//		boolean valid = true;
//		
//		for (int i = editors.size() - 1; i >= 0; i--)
//			valid &= ((XEditor)editors.get(i)).isValid();
//		
//		setValid(valid);
		
		boolean modified = true;
		
		for (int i = editors.size() - 1; i >= 0; i--)
			modified &= ((XEditor)editors.get(i)).isModified();
		
		setModified(modified);
	}
	
}
